/*
 * The National Parks Database Project
 * Group: Tyler Simrell, Vladmir Dudnic, Michael Jagielo
 * Maintenance.java
 */

package panels;

import gui.MainFrame;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This JPanel class creates the Maintenance menu that contains all of the actions that a 
 * maintenance person can do.
 * @author Tyler Simrell
 * @version 5/24/13
 */
@SuppressWarnings("serial")
public class MaintenancePanel extends JPanel implements ActionListener
{
	private CardLayout cards;
	private JPanel cardPanel;
	private JTextField updateTrailField;
	private JLabel updateLabel;
	private UpdateDBPanel updateDBPanel;
	private CheckStatusPanel csp;
	private JTextField statusCheckField;
	private String statusTrail;
	
	/**
	 * This constructor takes in the parameters necessary for communication between the main JPanel along with the
	 * JPanel that comes after this one.
	 * 
	 * @param passedInCards The card layout that all of the panels are held in.
	 * @param passedInCardPanel The JPanel that the card layout sits on.
	 * @param passedInUpdateTrailField The text field that the user puts the trail name they want to update into.
	 * @param passedInLabel The label that the trail name will go on in the update panel.
	 * @param passedInUpdateDBPanel The panel that allows the user to imput the changes to the specified trail.
	 */
	public MaintenancePanel(CardLayout passedInCards, JPanel passedInCardPanel, JTextField passedInUpdateTrailField, 
			JLabel passedInLabel, UpdateDBPanel passedInUpdateDBPanel, CheckStatusPanel passedInCheckStatusPanel)
	{
		super();
		cards = passedInCards;
		cardPanel = passedInCardPanel;
		updateTrailField = passedInUpdateTrailField;
		updateLabel = passedInLabel;
		updateDBPanel = passedInUpdateDBPanel;
		csp = passedInCheckStatusPanel;
		setUp();
	}
	
	/**
	 * This method sets up the GUI for the user. 
	 */
	void setUp()
	{
		cards.addLayoutComponent(this, MainFrame.MAINTENANCE_PANEL);
		this.setLayout(null);
		
		JLabel welcomeLabel = new JLabel("Welcome Maintenance");
		welcomeLabel.setBounds(331, 12, 195, 25);
		welcomeLabel.setFont(new Font("Tempus Sans ITC", Font.BOLD, 18));
		this.add(welcomeLabel);
		
		JLabel checkStatusLabel = new JLabel("Would you like to check the status of a trail?");
		checkStatusLabel.setFont(new Font("Times New Roman", Font.BOLD, 15));
		checkStatusLabel.setBounds(71, 126, 307, 16);
		this.add(checkStatusLabel);
		
		statusCheckField = new JTextField();
		statusCheckField.setToolTipText("Enter Trail Name Here");
		statusCheckField.setBounds(71, 154, 286, 20);
		this.add(statusCheckField);
		statusCheckField.setColumns(10);
		
		JButton checkStatusButton = new JButton("Status");
		checkStatusButton.setBounds(573, 151, 109, 26);
		checkStatusButton.addActionListener(this);
		
		this.add(checkStatusButton);
		
		JLabel makeChangesLabel = new JLabel("Would you like to update a trail in the database?");
		makeChangesLabel.setFont(new Font("Times New Roman", Font.BOLD, 15));
		makeChangesLabel.setBounds(71, 213, 345, 16);
		this.add(makeChangesLabel);
		
		updateTrailField.setBounds(71, 241, 252, 20);
		this.add(updateTrailField);
		updateTrailField.setColumns(10);
		
		JButton makeChangesButton = new JButton("Update");
		makeChangesButton.setBounds(573, 235, 109, 26);
		makeChangesButton.addActionListener(this);
		this.add(makeChangesButton);
		
		JLabel addLabel = new JLabel("Would you like to add a trail to the database?");
		addLabel.setFont(new Font("Times New Roman", Font.BOLD, 15));
		addLabel.setBounds(71, 300, 307, 16);
		this.add(addLabel);
		
		JButton addButton = new JButton("Add");
		addButton.setBounds(573, 295, 109, 26);
		addButton.addActionListener(this);
		this.add(addButton);
		
		this.add(new LowerPanel(cards, cardPanel, true));
	}

	/**
	 * This actionPeformed method services the Check Status button, which allows the user to check the status
	 * of a trail, the Update button, which allows the user to update a set trail if they put in a legitimate 
	 * string, and the Add button, which allows the user to add a trail. 
	 */
	@Override
	public void actionPerformed(ActionEvent event) 
	{
		JButton buttonPressed = (JButton) event.getSource();
		if(buttonPressed.getText().equals("Status"))
		{
			if(statusCheckField.getText().isEmpty())
			{
				JOptionPane.showMessageDialog(null, "You must enter a trail name to be able to check it.", 
						"Trail name error", JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				statusTrail = statusCheckField.getText();
				csp.createDataForTable(statusTrail);
				csp.revalidate();
				cards.show(cardPanel, MainFrame.CHECK_STATUS_PANEL);
			}
		}
		
		else if(buttonPressed.getText().equals("Update"))
		{
			if(updateTrailField.getText().isEmpty())
			{
				JOptionPane.showMessageDialog(null, "You must enter a trail name to be updated.", 
						"Trail name error", JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				updateLabel.setText("Update " + updateTrailField.getText());
				updateDBPanel.setUpFields(updateTrailField.getText());
				updateDBPanel.revalidate();
				cards.show(cardPanel, MainFrame.UPDATE_DB_PANEL);
			}
		}
		
		else if(buttonPressed.getText().equals("Add"))
		{
			cards.show(cardPanel, MainFrame.ADD_DB_PANEL);	
		}
	}
}
